package com.study.mike.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.study.mike.model.dto.GrantRoleResourceDTO;
import com.study.mike.model.dto.QueryRoleDTO;
import com.study.mike.model.entity.Role;
import com.study.mike.model.po.*;
import com.study.mike.result.R;
import com.study.mike.result.RUtil;
import com.study.mike.service.RoleService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 角色
 *
 * @author KangHui
 * @Date 2022/8/19 16:23
 */
@RequestMapping("/role")
@RestController
public class RoleController {
    @Resource
    private RoleService roleService;

    /**
     * 新增角色
     *
     * @param addRolePO
     * @return
     */
    @PostMapping("/add/role")
    public R<Role> addRole(@RequestBody @Valid AddRolePO addRolePO) {
        Role role = roleService.addRole(addRolePO);
        if (ObjectUtil.isNull(role)) {
            return RUtil.error("新增角色失败");
        }
        return RUtil.success(role);
    }

    /**
     * 分页获取角色信息
     *
     * @param rolePO
     * @return
     */
    @PostMapping("/query/role")
    public R<Page<QueryRoleDTO>> queryRole(@RequestBody QueryRolePO rolePO) {
        return RUtil.success(roleService.queryRole(rolePO));
    }

    /**
     * 编辑角色
     *
     * @param po
     * @return
     */
    @PostMapping("/edit/role")
    public R<Role> editRole(@RequestBody EditRolePO po) {
        Role role = roleService.editRole(po);
        if (ObjectUtil.isNull(role)) {
            return RUtil.error("编辑角色失败");
        }
        return RUtil.success(role);
    }

    /**
     * 角色授予资源列表(对应用户的多个角色-显示对应的权限)
     */
    @PostMapping("/grant/role/resource/list")
    public R<List<GrantRoleResourceDTO>> grantRoleResourceList(@RequestBody List<Integer> list) {
        return RUtil.success(roleService.grantRoleResourceList(list));
    }

    /**
     * 角色授予资源
     */
    @PostMapping("/grant/role/resource")
    public R grantRoleResource(@RequestBody GrantRoleResourcePO po) {
        roleService.grantRoleResource(po);
        return RUtil.success("success");
    }

    /**
     * 对角色查询结果按不同维度排序
     */
    @PostMapping("/sort/other")
    public R<Role> sortOther(@RequestBody SortOtherPO po) {
        return RUtil.success(roleService.sortOther(po));
    }


}
